package cn.stylefeng.guns.modular.rw.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.core.ParamUtil;
import cn.stylefeng.guns.modular.rw.entity.InfoSet;
import cn.stylefeng.guns.modular.rw.service.IInfoSetService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 描述：基本信息配置表 controller
 * 作者：chenhuiyan
 * 时间：2020-04-30
 */
@Controller
@RequestMapping("/rw/infoSet")
public class InfoSetController extends BaseController {

    private String PREFIX = "/modular/rw/infoSet/";

    @Autowired
    private IInfoSetService infoSetService;

    /**
     * 跳转到管理首页
     */
    @RequestMapping(value = "")
    public String index() {
        return PREFIX + "infoSet.html";
    }

    /**
     * 获取列表
     */
    @ResponseBody
    @RequestMapping(value = "/list")
    public Object list() {
        Map<String, Object> params = ParamUtil.getParams();
        Page<Map<String, Object>> list = infoSetService.list(params);
        return LayuiPageFactory.createPageInfo(list);
    }

    /**
     * 跳转添加/修改页面
     * @param infoSetId
     * @param model
     * @return
     */
    @RequestMapping(value = "/addOrEdit")
    public String addOrEdit(String infoSetId, Model model) {
        model.addAttribute("infoSetId", infoSetId);
        return PREFIX + "infoSet_add.html";
    }

    /**
     * 保存
     * @param infoSet
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save")
    public ResponseData save(InfoSet infoSet) {
        if (ToolUtil.isEmpty(infoSet.getId())) {
            infoSet.preInsert();
            infoSetService.save(infoSet);
        }else{
            infoSet.preUpdate();
            infoSetService.updateById(infoSet);
        }
        return SUCCESS_TIP;
    }

    /**
     * 获取详情
     * @param infoSetId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getInfoById/{infoSetId}")
    public Object getInfoById(@PathVariable("infoSetId") String infoSetId){
        InfoSet infoSet = infoSetService.getById(infoSetId);
        return infoSet;
    }

}
